package com.atguigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.atguigu.utils.JwtHelper;
import com.atguigu.utils.MD5Util;
import com.atguigu.utils.Result;
import com.atguigu.utils.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-08-16 14:36:55
*/
@Service
@SuppressWarnings("all")
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;
    /**
     * 1.根据账号，查找用户对象，-loginUser
     * 2.如果用户对象为空，查询失败，账号错误！501
     * 3.账号对比成功之后，进行密码对比，失败返回503
     * 4，都成功根据用户id生成一个token->result返回
     * @param user
     * @return
     */
    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser = userMapper.selectOne(queryWrapper);
        if (loginUser == null){
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }
        //对比密码
        if (!StringUtils.isEmpty(user.getUserPwd()) &&
                MD5Util.encrypt(user.getUserPwd())==loginUser.getUserPwd()){
            return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
        }else {
            //根据ID生成token，并将其封装到result返回即可
            String token = jwtHelper.createToken(Long.valueOf(loginUser.getUid()));
            HashMap map = new HashMap<>();
            map.put("token",token);
            return Result.ok(map);
        }
        //密码错误
        //return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }

    /**
     * 1.校验是否在有效期token
     * 2.根据token解析userID
     * 3. 根据用户id查询用户数据
     * 4.去掉密码封装result结果返回即可
     * @param token
     * @return
     */
    @Override
    public Result getUserInfo(String token) {
        if (jwtHelper.isExpiration(token) == false){
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        int userId = jwtHelper.getUserId(token).intValue();
        User user = userMapper.selectById(userId);
        user.setUserPwd("");
        Map map = new HashMap<>();
        map.put("loginUser",user);
        return Result.ok(map);
    }

    /**
     * 检查用户账号是否可用
     * count大于0表示不可用   count等于0表示可用
     * @param username
     * @return
     */
    @Override
    public Result checkUsername(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        Long count = userMapper.selectCount(queryWrapper);
        if (count==0){
            return Result.ok(null);
        }
        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }

    /**
     * 检查账号是否被注册
     * 密码加密处理
     * 账号数据保存
     * 返回结果
     * @param user
     * @return
     */
    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        Long count = userMapper.selectCount(queryWrapper);
        if (count > 0){
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));
        userMapper.insert(user);
        return Result.ok(null);
    }
}




